package com.wanpinghui.wphlibrary.photovideo.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.MediaController;
import android.widget.VideoView;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2016/12/28
 * 版本:1.0
 ***/

public class MoviePlayerView extends VideoView {

    public MoviePlayerView(Context context) {
        this(context, null);
    }

    public MoviePlayerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoviePlayerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    /**
     * 播放视频
     */
    public void play(String path, final OnPlayCompletionListener completionListener) {
        //本地的视频 需要在手机SD卡根目录添加一个 fl1234.mp4 视频
        Uri uri = Uri.parse(path);
        //设置视频控制器
        setMediaController(new MediaController(getContext()));
        //播放完成回调
        setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (completionListener != null) {
                    completionListener.onPlayCompletion();
                }
            }
        });
        //设置视频路径
        setVideoURI(uri);
        //开始播放视频
        start();

    }

    private void stop() {
        stop();
    }

    /**
     * 释放资源
     */
    public void release() {
        release();
    }


    /**
     * 播放成功回调
     *
     * @author liuyinjun
     * @date 2015-2-9
     */
    public interface OnPlayCompletionListener {
        void onPlayCompletion();
    }

}
